package org.apache.hadoop.mapred;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import org.apache.hadoop.util.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableUtils;

class ExchangeResponse implements Writable, Configurable {
    Vector<TaskTrackerStatus> taskStatus;
  public static final Log LOG = LogFactory.getLog("org.apache.hadoop.mapred.ExchangeResponse");

    ExchangeResponse(){
        LOG.info("here");
    }

    ExchangeResponse(HashMap<String, TaskTrackerStatus> taskTrackers){
        // convert from map to array
    taskStatus = new Vector<TaskTrackerStatus>();
    for (Iterator it = taskTrackers.values().iterator(); it.hasNext();) {
      TaskTrackerStatus jip = (TaskTrackerStatus) it.next();
      taskStatus.add(jip);
    }
  }

    public  Vector<String> getTaskHosts()
    {
        Vector<String> hosts = new Vector<String>();
        for (Iterator it = taskStatus.iterator(); it.hasNext();) {
            TaskTrackerStatus tt = (TaskTrackerStatus) it.next();
            String host = tt.getHost();
            LOG.info("get host name " + host);
            hosts.add(host);            
        }
        return hosts;
    }

    public int getNumTasks()
    {
        if (taskStatus == null) 
            return 0;
        else
            return taskStatus.size();
    }

  Configuration conf = null;  
  public void readFields(DataInput in) throws IOException {
      int size = in.readInt();
      taskStatus = new Vector<TaskTrackerStatus>();
      while (size > 0)
          {
              TaskTrackerStatus tt = new TaskTrackerStatus();
              tt.readFields(in);
              taskStatus.add(tt);
              size--;
              }
  }

  public void write(DataOutput out) throws IOException {
      if (taskStatus == null) {
          out.writeInt(0);
      } else {

          out.writeInt(taskStatus.size());

          for (Iterator it = taskStatus.iterator(); it.hasNext();) {
              TaskTrackerStatus tt = (TaskTrackerStatus) it.next();
              tt.write(out);
          }
      }
      /*    out.writeShort(responseId);
    out.writeInt(heartbeatInterval);
    if (actions == null) {
      WritableUtils.writeVInt(out, 0);
    } else {
      WritableUtils.writeVInt(out, actions.length);
      for (TaskTrackerAction action : actions) {
        WritableUtils.writeEnum(out, action.getActionId());
        action.write(out);
      }
      }*/
    //ObjectWritable.writeObject(out, actions, actions.getClass(), conf);
  }
    
  public Configuration getConf() {
    return conf;
  }

  public void setConf(Configuration conf) {
    this.conf = conf;
  }

}